package com.kenyi.co.ui.classification.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.github.jdsjlzx.ItemDecoration.DividerDecoration;

import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.google.gson.Gson;
import com.kenyi.co.R;
import com.kenyi.co.okhttp.OkHttpUtils;
import com.kenyi.co.okhttp.callback.StringCallback;
import com.kenyi.co.ui.classification.bean.CategoryListBean;
import com.kenyi.co.ui.classification.bean.CategoryMoreBean;
import com.kenyi.co.utils.ListBaseAdapter;
import com.kenyi.co.utils.LogU;
import com.kenyi.co.utils.NetConfig;
import com.kenyi.co.utils.SuperViewHolder;
import com.kenyi.co.utils.ToastUtil;
import com.kenyi.co.view.GridItemDecoration;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * Created by Administrator on 2018/1/29.
 */

public class ClassificationAdapter extends ListBaseAdapter<CategoryListBean.DatasBean> {
    ArticlesHotAdapter mDataAdapter = null;
    LRecyclerView recyclerView= null;
    public ClassificationAdapter(Context context) {
        super(context);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_fenlei_home_title_r;
    }

    @Override
    public void onBindItemHolder(SuperViewHolder holder, int position) {
        CategoryListBean.DatasBean item = mDataList.get(position);
        TextView textView = holder.getView(R.id.info_text);

         recyclerView = holder.getView(R.id.recyclerview_inner);
        if (position == 0) {
            textView.setText("人气视频");
            recyclerView.setLayoutManager(new GridLayoutManager(mContext, 2));
//            recyclerView.setAdapter(new ArticlesAdapter(mContext, item.getArticles()));
            GridItemDecoration divider = new GridItemDecoration.Builder(mContext)
                    .setVertical(R.dimen.common_20dp)
                    .setColorResource(R.color.white)
                    .build();
            recyclerView.addItemDecoration(divider);
            recyclerView.setAdapter(new LRecyclerViewAdapter(new ArticlesAdapter(mContext, item.getArticles())));


            recyclerView.setLoadMoreEnabled(false);
        } else {

            mDataAdapter = new ArticlesHotAdapter(mContext, item.getArticles());
            DividerDecoration divider = new DividerDecoration.Builder(mContext)
                    .setHeight(R.dimen.common_30dp)
                    .setColorResource(R.color.white)
                    .build();
            recyclerView.addItemDecoration(divider);
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
            textView.setVisibility(View.GONE);
            recyclerView.setPullRefreshEnabled(false);
            recyclerView.setAdapter(new LRecyclerViewAdapter(mDataAdapter));
            recyclerView.setLoadMoreEnabled(true);
            recyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
                @Override
                public void onLoadMore() {
                    getCategoryMore("北京",1);
                }
            });
        }
    }
    @Override
    public void onBindItemHolder(SuperViewHolder holder, int position, List<Object> payloads) {

        List<CategoryListBean.DatasBean.ArticlesBean> listitem = (List<CategoryListBean.DatasBean.ArticlesBean>)(List)payloads.get(0);
        if (payloads.isEmpty()) {

        } else {

            mDataAdapter.addAll(listitem);
            LRecyclerView recyclerView = holder.getView(R.id.recyclerview_inner);
            recyclerView.refreshComplete(10);
        }

    }
    public void getCategoryMore(String tags, final int page) {

        Gson gson = new Gson();
        Map<Object, Object> map = new HashMap<>();

        map.put("pagecount", page);
        String str = gson.toJson(map);
        String url = NetConfig.CATEGORY_MORE + "?tags=" + tags;

        OkHttpUtils.postString()
                .url(url)
                .content(str)
                .addHeader("Content-Type", "application/json")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {


                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Gson gson = new Gson();

                        CategoryMoreBean packlist = gson.fromJson(response, CategoryMoreBean.class);
                        int code = packlist.getCode();
                        List<CategoryListBean.DatasBean.ArticlesBean> itemListharticle;



                        if (code == 200) {
                            itemListharticle = packlist.getDatas();
                            mDataAdapter.addAll(itemListharticle);

                             recyclerView.refreshComplete(10);
//LRecyclerView recyclerView
//                            if(itemListharticle.size()>0){
//
//                                mLRecyclerViewAdapter.notifyItemChanged(mLRecyclerViewAdapter.getAdapterPosition(false,1), itemListharticle);
//                            }else{
//
//                                mRecyclerView.refreshComplete(10);
//
//                            }



                        }
                    }
                });
    }
}
